from flask import render_template, session, request, jsonify
from models import db
from models.index import Article, User, Classify
from . import index_blu


@index_blu.route("/")
def hello():
    return render_template('index/hello.html')


@index_blu.route("/index")
def index():
    # 用户头像  查询用户是否已登录
    user_id = session.get("user_id", 0)
    if user_id != 0:
        user = db.session.query(User).filter(User.id == user_id).one()
        ava = user.avatar_url
    else:
        user = None
        ava = "../static/img/user.png"
    # 左侧列表  查询所有分类
    classify = db.session.query(Classify).all()
    # 文章显示  查询更新时间最近的五条内容
    article = db.session.query(Article).order_by(-Article.update_time).limit(1)
    return render_template('index/index.html', ava=ava, user=user, classify=classify, article=article)


@index_blu.route("/article_list")
def category_news():
    page, classify = request.args.get("page", 1), int(request.args.get("classify", 0))
    if classify == 0:
        paginate = db.session.query(Article).filter(Article.status == 1) \
            .order_by(-Article.create_time).paginate(page=int(page), per_page=5, error_out=False)
    else:
        paginate = db.session.query(Article).filter(Article.classify == classify, Article.status == 1) \
            .order_by(-Article.create_time).paginate(page=int(page), per_page=5, error_out=False)
    ret = {
        "pagess": paginate.pages,  # 总页数
        "article_list": [a.to_dict() for a in paginate.items]
    }
    return jsonify(ret)


@index_blu.route("/to_detail/<int:art_id>")
def to_detail(art_id):
    # 用户头像
    user_id = session.get("user_id", 0)
    if user_id != 0:
        user = db.session.query(User).filter(User.id == user_id).one()
        ava = user.avatar_url
    else:
        user = None
        ava = "../static/img/user.png"
    # 查询文章
    article = db.session.query(Article).filter(Article.id == art_id).first()
    # 访问量+1
    article.clicks += 1
    db.session.commit()
    # 查询类别
    classify = db.session.query(Classify).filter(Classify.id == article.classify).first()
    # 查询作者
    writer = article.user
    # 用户是否关注作者
    follower = [x.id for x in writer.fans]
    if user_id in follower:
        writer.can_follow = False  # 已关注
    else:
        writer.can_follow = True  # 未关注

    return render_template('index/detail.html', article=article, classify=classify, ava=ava, writer=writer, user=user)


@index_blu.route("/to_writer/<int:writer_id>")
def to_writer(writer_id):
    user_id = session.get("user_id", 0)
    if writer_id == user_id:
        user = db.session.query(User).filter(User.id == user_id).first()
        return render_template('index/user.html', user=user)
    writer = db.session.query(User).filter(User.id == writer_id).one()
    article = db.session.query(Article).filter(Article.user_id == writer_id).order_by(-Article.update_time).all()

    # 用户头像
    if user_id != 0:
        user = db.session.query(User).filter(User.id == user_id).one()
        ava = user.avatar_url
    else:
        user = None
        ava = "../static/img/user.png"

    return render_template('index/writer.html', user=user, writer=writer, article=article, ava=ava)
